;'use strict';
/**
 * <p>
 *   Description: UserModifyEx 可编辑的 div
 * </p>
 * @author 袁兴旺
 * @version 1.0
 * @date 2019-11-18 17:22
 */
(function(){

  if(window.UserModifyEx){
    return;
  }

  var UserModifyEx = window.UserModifyEx = function(ele){
    this.element = ele;
    this.range = undefined;

    this.init();
  };

  var prototype = UserModifyEx.prototype;

  /**
   * 获取可编辑域实例
   * @param ele
   */
  UserModifyEx.getInstance = function(ele){
    if(!ele) {
      return null;
    }

    if(isString(ele)) {
      ele = document.getElementById(ele);

      if(!ele) {
        return null;
      }
    }

    return new UserModifyEx(ele);
  };

  /**
   * 初始化
   */
  prototype.init = function(){

    var _this = this;

    this.element.contentEditable = 'true';

    PageUtils.addClass(this.element, [
      'content-editable-ex'
    ]);

    // 禁止拖拽
    this.element.addEventListener('dragstart', function(e) {
      e.preventDefault();
      return false;
    });

    // 禁止换行
    this.element.addEventListener('keydown', function(e) {
      var key = e.key;
      if(key === 'Enter') {
        e.preventDefault();
      }
    });

    // 处理粘贴的数据
    this.element.addEventListener('paste', function(e) {

      var clipboardData = e.clipboardData;
      if (!clipboardData) {
        return;
      }
      e.preventDefault();

      var text = clipboardData.getData('text');
      if (!window.getSelection || text === null || text === '' ) {
        return;
      }

      var textNode = document.createTextNode(text);

      var selection = getSelection();
      var range = selection.getRangeAt(0);

      range.deleteContents();
      range.insertNode(textNode);

      _this.setRangeToEnd(selection);
    });
  };

  /**
   * 设置空白内容的提示
   * @param placeholder
   * @returns {Window.UserModifyEx.placeholder}
   */
  prototype.placeholder = function(placeholder){
    this.element.placeholder = placeholder;
    return this;
  };

  /**
   * 获取 Selection
   * @returns {Selection}
   */
  prototype.getSelection = function(){
    return getSelection();
  };

  prototype.setRangeToEnd = function(selection) {
    selection.selectAllChildren(this.element);
    selection.collapseToEnd();
  };

  /**
   * 把光标移到富文本框最后
   * @param selection
   * @returns {Range}
   */
  prototype.getRangeOfReset = function(selection){
    this.setRangeToEnd(selection);
    return selection.getRangeAt(0);
  };

  /**
   * 获取 Range
   * @param selection
   */
  prototype.getRange = function(selection){
    if(!selection){
      selection = this.getSelection();
    }

    var range;

    var anchorNode = selection.anchorNode;
    if(!anchorNode || (anchorNode !== this.element && anchorNode.parentNode !== this.element)){
      // 选择的标签不在富文本框范围内

      range = this.getRangeOfReset(selection);
    }else if(selection.getRangeAt && selection.rangeCount > 0){

      range = selection.getRangeAt(0);
    }else{
      // 未选定

      range = this.getRangeOfReset(selection);
    }

    this.range = range;
    return range;
  };

  /**
   * 添加标签
   * @param element
   * @returns {boolean}
   */
  prototype.addElement = function(element){

    if(!element){
      return false;
    }

    element.contentEditable = 'false';

    var range = this.getRange();

    range.insertNode(element);
    range.setStartAfter(element);
    range.collapse(true);

    return true;
  };

})();
